package com.seeingtv.controller;

import com.seeingtv.common.utils.StringUtils;
import com.seeingtv.controller.base.BaseController;
import com.seeingtv.model.Menu;
import com.seeingtv.model.User;
import com.seeingtv.service.MenuService;
import com.seeingtv.vo.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author hubert
 */
@Controller
public class SystemController extends BaseController{

	@Resource
	private MenuService menuService;

	/**
	 * 方式一 ModelAndView 指定路径
	 */
    @GetMapping("/login")
    public String login(){
        //页面路径
        return "system/login";
    }

	@PostMapping("system/login")
	@ResponseBody
	public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe) {
		UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			return success();
		} catch (AuthenticationException e) {
			String msg = "用户或密码错误";
			if (StringUtils.isNotEmpty(e.getMessage())) {
				msg = e.getMessage();
			}
			return error(msg);
		}
	}

    /**
	 * 方式二 string 指定路径
	 */

    @RequestMapping("system/index")
	public ModelAndView toIndex(){
		log.info("页面定向");
		ModelAndView mv = getModelAndView();
		User user = getUser();
		String menuIds = menuService.selectMenuIdsByRoleId(Long.valueOf(3));
		String[] array = menuIds.split(",");
		List<Menu> menus = menuService.selectMenusById(array);

		mv.addObject("menus", menus);
		mv.addObject("user", user);
		mv.addObject("copyrightYear", "2018");
		//页面路径
		mv.setViewName("system/index");

		/*Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		System.out.println("shiro session:"+session);
		System.out.println("shiro session:"+session.getId()+","+session.getAttribute("LoginName"));

		HttpSession session2 = getRequest().getSession();
		System.out.println("severlet session:"+session2);
		System.out.println("severlet session:"+session2.getId()+","+session2.getAttribute("LoginName"));*/
		//SecurityUtils.getSubject().isPermitted("view");

		return mv;
	}

	/*@RequestMapping("/index")
	public String toIndex(ModelMap modelMap){
		String menuIds = menuService.selectMenuIdsByRoleId(Long.valueOf(3));
		String[] array = menuIds.split(",");
		List<Menu> menus = menuService.selectMenusById(array);

		modelMap.put("menus", menus);
		modelMap.put("user", "");
		modelMap.put("copyrightYear", "2018");
		return "system/index";
	}*/

	@RequestMapping("system/main")
	public ModelAndView toMain(){
		log.info("页面定向");
		ModelAndView mv = getModelAndView();
		//页面路径
		mv.setViewName("system/main");
		return mv;
	}


}
